Base = {};

Base.ajax = function (option) {
    var url = option.url;
    var async = option.async;
    var data = $.extend({}, option.data);
    var timeout = option.timeout || 20000;
    var successfn = option.success;
    var errorfn = option.error;
    var completefn = option.complete;
    var loading = option.loading;
    if (typeof(loading) == "undefined") {
        loading = true;
    }

    data["date"] = new Date().getTime(); // 增加时间戳，防止缓存

    $.ajax({
        type: "post",
        async: async,
        data: data,
        url: url,
        dataType: "json",
        timeout: timeout, //超时时间设置，单位毫秒
        success: function (d) {
            successfn(d);
        },
        error: function (jqXHR, status, errorThrown) { //请求异常后
            if (status == 'timeout') {
                console.error("请求超时，请重新提交！");
            }

            console.error(status);
            console.error(errorThrown);

            tiperror("请求响应异常，请重新提交！")
            if (errorfn) {// 回调自定义方法
                errorfn();
            }
        },
        beforeSend: function (XMLHttpRequest) {
            if (loading) {
                Base.loading();
            }
        },
        complete: function (XMLHttpRequest, textStatus) {
            if (loading) {
                Base.loadingClose();
            }

            if (completefn) {
                completefn();
            }
        }
    });
}

Base.loading = function (content, type) {
    // 防止ajax意外被中断导致一直显示加载
    top.$(".base-tip").remove();

    var _o = top.$("body");

    content = content || '正在查询中，请稍等......';
    var s = '<div class="base-tip">' +
        '<div class="base-tip-bg"></div>' +
        '<div class="base-tip-box">' +
        '<div class="base-tip-box-in">' + content + '</div>' +
        '</div>' +
        '</div>';

    _o.append(s);
}

Base.loadingClose = function () {
    top.$(".base-tip:first").remove();
}

$.fn.serializej = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

$(document).ajaxComplete(function (whoareyou, request, settings) {
    if (request != undefined) {
        var data = request.responseText;
        if (data && data.length > 0) {
            if (data.indexOf("ajax404ajax") != -1) {
                window.location.href = "/404";
            } else if (data.indexOf("ajaxPwdModifyajax") != -1) {
                top.window.location.href = "";
            } else if (data.indexOf("ajaxSessionOutajax") != -1) {
                top.window.location.href = "/login";
            }
        }
        data = null;
    }
});

Base.executeExport = function (url, param) {
    Base.ajax({
        url: url,
        data: param,
        success: function (data) {
            if (data.code) {
                var downUrl = '/common/download?file=' + data.msg;
                window.open(downUrl);
            } else {
                tiperror("文件准备失败，请重新提交请求");
            }
        },
        error: function () {
            tiperror("文件准备失败，请重新提交请求");
        }
    });
}

Base.reload = function () {
    setTimeout(function () {
        window.location.reload();
    }, 1200);
}

function tiperror(msg) {
    top.$.notify({
        // options
        icon: 'iconfont icon-cuowu1',
        message: msg
    }, {
        // settings
        type: 'danger',
        position: 'fixed',
        placement: {
            from: "top",
            align: "center"
        },
        animate: {
            enter: 'animated fadeInDown',
            exit: 'animated fadeOutUp'
        },
        z_index: 100000,
        delay: 2000
    });
}

function tipsuccess(msg) {
    top.$.notify({
        // options
        icon: 'iconfont icon-zhengque1',
        message: msg
    }, {
        // settings
        type: 'success',
        position: 'fixed',
        placement: {
            from: "top",
            align: "center"
        },
        animate: {
            enter: 'animated fadeInDown',
            exit: 'animated fadeOutUp'
        },
        z_index: 100000,
        delay: 2000
    });
}

function hideLoading(obj) {
    $(obj).removeClass("loading");
}

function randomNum(min, max) {
    var ran = max - min;
    var r = Math.random();
    return (min + Math.round(r * ran));
}

function saveTheme(theme) {
    Base.ajax({
        "url": "/common/theme/save",
        "data": {
            "theme": theme
        },
        "success": function (data) {
            if (data.code && data.data) {
                Base.reload();
            } else {
                tiperror("切换主题失败，请重新切换");
            }
        }
    });
}